package com.league.zjxn.main.home

import android.os.Bundle
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.HomeViewModel
import com.league.zjxn.main.adapter.HomeTrendData1Adapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_home_trend.*

class HomeTrendFragment : OwnerViewModeFragment<HomeViewModel>() {

    companion object {
        private const val KEY_HOME_TREND_ID = "KEY_HOME_TREND_ID"
        fun newInstance(type: Int): HomeTrendFragment {
            val bundle: Bundle = Bundle().apply {
                putInt(KEY_HOME_TREND_ID, type)
            }
            return HomeTrendFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mHomeTrendData1Adapter = HomeTrendData1Adapter()

    private val mType: Int
        get() = arguments?.getInt(KEY_HOME_TREND_ID) ?: 0

    override fun viewModel(): HomeViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_home_trend

    override fun initView() {
        rvHomeTrendDataBody.adapter = mHomeTrendData1Adapter
    }

    override fun initData() {
        viewModel.homeTrendData(mType)
    }

    override fun initViewMode() {
        viewModel.trendData1LiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.data
                ?: return@observe
            mHomeTrendData1Adapter.setNewInstance(data)
        })
    }

}